<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
  
    public function __construct()
    {
        parent::__construct();
		//echo $this->session->userdata('is_logged_in');
        $is_logged_in = $this->session->userdata('is_logged_in');
		if (!$is_logged_in){
			redirect('login');
		}
		$base = $data['base'] = $this->config->item('base_url');
		$base .= 'index.php/';
    }
    function index(){
        $this->load->view('admin/index');   
    }

    public function add_job()
    { 
         $this->load->view('admin/add_job');
         
    }
    public function customer_search()
    {   

        if (isset($_POST['customer']) and ($_POST['customer']) != '' ){
            $string = $_POST['customer'];
            $data['result'] = $this->customer_model->search_customer($string);
            $this->load->view('admin/customer/search_results.php', $data);
            
        }else{
            //redirect('admin/customer_search');
            $this->load->view('admin/customer/request');
            //echo 'looking for the customer';
        }
        
    }
    public function customer_list(){
         $data['result'] = $this->customer_model->get_all_customers();
         $this->load->view('admin/customer/search_results.php', $data);
    }
    public function customer_add(){
        $this->load->view('admin/customer/customer_add');
    }
	public function create_job(){
		$ident = rand(1, 99999);
		redirect('admin/job_generate/'.md5($ident));
		
	}
	public function job_generate($function = FALSE){
		if($this->uri->segment(3)){			
			$iden = $this->uri->segment(3);
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								(rewriting table from session) 
					 * 			storing the table:
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/			
			$table_reader = $this->session->userdata($iden);
			//////////////////////////Default link:////////////////////////////
			$data['link'] = 'type'; 
			if($this->uri->segment(4)){
				
				//////////////////////////PICK UP THE DATE FOR THREE DAYS AHEAD////////////////////////////
				$selected_date = $this->session->userdata($iden);
				if(!isset($selected_date['time'])){
					$table_reader['time'] = workDaysNextDate(date('d-m-Y'), 2);;	
				}
				//////////////////////////END PICKUP THE DATE FOR THREE DAYS AHEAD////////////////////////////
				
				//////////////////////////GENERATE ERROR MESSAGE ON CUSTOMER CREATEION SCREEN////////////////////////////
				//////////////////////////ONLY IN CASE IF CUSTOMER HAS ALREADY BEEN SELECTED////////////////////////////
				if (isset($table_reader['customer'])){$data['customer_already_selected']=TRUE;};
				//////////////////////////END ERROR MESSAGE GENERATION////////////////////////////////////////////
				switch($this->uri->segment(4)){
						
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								job type screen:
						
					//////////////////////////////////////////////////////////////////////////////////////////////*/						
						
					case 'type':
						if (isset($table_reader['type'])){
							$data['type_repopulate'] = $table_reader['type'];
						}
						$data['link'] = 'type';	
						
					break;	
					
						
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								job type has been selected:
						
					//////////////////////////////////////////////////////////////////////////////////////////////*/

			
					case 'type_submit':
						//$selected [$iden] ['type'] = $_POST['job_type'];
						//$this->session->set_userdata($iden,$selected);
						$table_reader ['type'] = $_POST['job_type'];
						$this->session->set_userdata($iden ,$table_reader);
						redirect('admin/job_generate/'.$iden.'/customer_select');
						break;
						
					
	
	
		
					
						
					/*////////////////////////////////////////////////////////////////////////////////////////////						

								options before selecting the customer:
						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
				
					
					case 'customer_select':
						$business_list = $this->customer_model->get_business_list();
						$data['business_list'] = $business_list;
						$titles = $this->customer_model->get_title_list();
						$data['titles'] = $titles;
						$data['link'] = 'customer_select';	
						
						if (isset($_POST['customer']) and ($_POST['customer']) != '' ){
							$string = $_POST['customer'];
							if(isset($_POST['business'])){
								$data['result'] = $this->customer_model->search_business_customer($string);
								$data['is_business_customer'] = TRUE;
							}else{
						        $data['result'] = $this->customer_model->search_customer($string);	
								$data['is_business_customer'] = FALSE;
								
							}
						
						}
					break;
					
						
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Customer has been selected from the search panel:
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
					
					
					
					case 'customer_submit':
						
						$selected = $this->uri->segment(5);
						$table_reader['customer']= $selected;
						$data['link'] = 'description';	

						
						
					break;
					
					
					
					
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Customer creation has been submited
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	



										
					case ('customer_select_create'):
						$this->form_validation->set_rules('title','Title', 'required');
						$this->form_validation->set_rules('name','Customer\'s Name', 'required');
						$this->form_validation->set_rules('phone','Phone number', 'required|is_natural');
						$this->form_validation->set_rules('phone2','Phone number 2', 'is_natural');
						$this->form_validation->set_rules('email','Email', 'valid_email');
						//if this is not a business customer
						if ($_POST['business_name']==0){
							$this->form_validation->set_rules('field1','Address', 'required');
							$this->form_validation->set_rules('field2','Address line two', '');
							$this->form_validation->set_rules('postcode','Postcode', 'required');
						}
						
						
						if ($this->form_validation->run()==FALSE){
							$business_list = $this->customer_model->get_business_list();
							$data['business_list'] = $business_list;
							$titles = $this->customer_model->get_title_list();
							$data['titles'] = $titles;
							$data['link'] = 'customer_select';
						}else{
								//stick business customer to the db:
							if($_POST['business_name']){
								if ($insert = $this->customer_model->insert_business_customer(
								//customer details:
								$_POST['title'],
								$_POST['name'],
								$_POST['phone'],
								$_POST['email'],
								$_POST['business_name']
								)){
									$table_reader['customer']= $insert;
									$data['link'] = 'description';
								}else{
									echo 'something went wrong :( please contact your admin';
								};
								
							}else
							//stick normal customer to the db:
							{
								if ($insert = $this->customer_model->insert_customer(
								//customer details:
								$_POST['title'],
								$_POST['name'],
								$_POST['phone'],
								$_POST['email'],
								//address details:
								$_POST['field1'],
								$_POST['field2'],
								$_POST['postcode'],
								1
								)){
									$table_reader['customer']= $insert;
									$data['link'] = 'description';
								}else{
									echo 'something went wrong :( please contact your admin';
								};
							}
						
							
							
							
						}
						
					break;


					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Job description screen:
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
					
					case('description'):
						if (isset($table_reader['descr'])){
							$data['description_repopulate'] = $table_reader['descr'];
						}
						$data['link'] = 'description';	
						
					
					break;
					
					
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Job description has been put:
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
					
	
					case 'submit_job_descr':
						$selected = $_POST['job_descr'];
						//echo $selected;
						if (strlen($selected)>0){
							

							
							$table_reader['descr'] = $selected;
							$this->session->set_userdata($iden, $table_reader);
							redirect('admin/job_generate/'.$iden.'/items');
							
						} 
						//if nothing selected, then reshow the form
						redirect('admin/job_generate/'.$iden.'/description');
						
					break;
					

					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Time has been submited
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
										
					case 'time':
						//$selected = $_POST['time'];
						$selected = $this->session->userdata($iden);
						if(!isset($selected['time'])){
							$table_reader['time'] = workDaysNextDate(date('d-m-Y'), 3);;
							
						}
						if(isset($_POST['time'])){
							if (strlen($_POST['time']) > 0){
								echo $_POST['time'];
								$table_reader['time'] = $_POST['time'];
								//echo 'time was selected from post value';
							}
						}
						//echo 'nothing';
						$data['link'] = 'time';
						
						//redirect('admin/job_generate/'.$iden.'/time');
					break;
						
						
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Time has been submited, list of items to be populated:
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/						
						
						
					case 'items':
						if(isset($_POST['time'])){
							if (strlen($_POST['time']) > 0){
								echo $_POST['time'];
								$table_reader['time'] = $_POST['time'];
								//echo 'time was selected from post value';
							}
						}
						if (isset($table_reader['items'])){
							$data['repopulated_items'] = $table_reader['items'];
						}					
						
						$data['link'] = 'items';
	
					break;	
					
										
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Items have been submited
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
							
					case 'items_submit';						
						$table_reader['items']=array(
							'item1' => $_POST['item1'],
							'item2' => $_POST['item2'],
							'item3' => $_POST['item3']
						);		
						$data['link']='payment';				
					break;	
					
					
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Payments screen
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
							
					case 'payment';		
					
						if(isset($table_reader['payment'])){
							
							for ($i=1; $i<=10; $i++){
												
								$data['repopulated_charge_items'][$i] = $table_reader['payment'][$i];
								$data['repopulated_charge_items'][$i.'v'] = $table_reader['paymentv'][$i];
								$data['repopulated_charge_items'][$i.'q'] = $table_reader['paymentq'][$i];
							}
						}
						if(isset($table_reader['min_charge'])){
							$data['min_charge']=$table_reader['min_charge'];
							$data['optional_fee']=$table_reader['optional_fee'];
							$data['planned_hours']=$table_reader['planned_hours'];
							$data['hours_count']=$table_reader['hours_count'];
							
						}	
						$data['link']='payment';				
					break;	
										
					
					
					
					
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Payments have been submitted
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
							
					case 'payment_submit';		
					
						for ($i=1; $i<=10; $i++){				
							$table_reader['payment'][$i]=$_POST['payment'.$i];
							$table_reader['paymentv'][$i]= $_POST['paymentv'.$i];
							$table_reader['paymentq'][$i]= $_POST['paymentq'.$i];
							
						}	
						$table_reader['min_charge']=$_POST['min_charge'];
						$table_reader['optional_fee']=$_POST['optional_fee'];
						$table_reader['planned_hours']=$_POST['planned_hours'];
						$table_reader['hours_count']=$_POST['hours_count'];
						
						
						
						$data['link']='validate';				
					break;	
					
					
					
					
					
					
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								Payments have been submitted
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
							
					case 'generate';		
					
						echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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////////////////////////////////////////////////////////////////////////
							
								Validate the entry
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
							
					case 'validate';
						
						if(isset($table_reader['time']) && (strlen($table_reader['time']) >= 3)) {
							$aDate_parts = preg_split("/[\s-]+/", $table_reader['time']);
							if (checkdate(
						        $aDate_parts[1], // Month
						        $aDate_parts[0], // Day
						        $aDate_parts[2] // Year
						    ))
						    {
						    	if(isset($table_reader['customer']) && (strlen($table_reader['customer']) >= 1)){
						    		if(isset($table_reader['type'])){
										if(isset($table_reader['descr']) && (strlen($table_reader['descr'])>=1)){
											if(isset($table_reader['min_charge'])){
												$data['confirm_button'] = TRUE;
											}else{
												$data['error'] = 'Please verify your payments... EXITING';
											}	
										}else{
											$data['error'] = 'Job description is void... EXITING';
										}	
									}else{
										$data['error'] = 'Job type was not selected... EXITING';
									}			    		
						    	}else{
						    		$data['error'] = 'Customer is not selected... EXITING';
						    	}
							}
						    else{
						    	$data['error'] = 'Date could not be validated... EXITING';
							};
							
						}
						$data['link']='validate';
					
					break;
					
										
					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								default action
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/	
					
						
					default:
							if ($this->session->userdata('creation')){
								$data['creation'] = $this->session->userdata('creation');
							}
						$data['link'] = $this->uri->segment(4);	
						//echo 'dupa';
				}

			}

					/*////////////////////////////////////////////////////////////////////////////////////////////
							
								This line is not always required. Most of the cases from over will redirect to the view automatycally.
					 * 						
					//////////////////////////////////////////////////////////////////////////////////////////////*/
			$this->session->set_userdata($iden ,$table_reader);
			$selected_date = $this->session->userdata($iden);
			$data['selected_date']=$selected_date;
			$this->load->view('admin/job/create_job', $data);
			
		}else{
				$this->create_job();
			}
		
		}
		/*////////////////////////////////////////////////////////////////////////////////////////////
				
					END job_generate()
			
		//////////////////////////////////////////////////////////////////////////////////////////////*/
		
			
	
    

}

